package com.spring.controller.javastack.request;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by chenhs on 2018/6/12.
 */
@Controller
public class Fecth4Controller {

    //存储已有参数，用于判断是否重复，从而判断线程是否安全
    public static Set<String> set = new HashSet<>();

    @RequestMapping("/test4")
    public void test() throws InterruptedException {
        HttpServletRequest request = ((ServletRequestAttributes)(RequestContextHolder.currentRequestAttributes())).getRequest();

        String value = request.getParameter("key");

        if (set.contains(value)) {
            System.out.println(value + "\t重复出现，request并发不安全！");
        } else {
            System.out.println(value);
            set.add(value);
        }

        //模拟线程执行了一段时间
        Thread.sleep(1000);
    }
}
